package com.dimong.www.loan.ui.frag.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.dimong.loan.sdk.app.BaseApplication;
import com.dimong.loan.sdk.ui.frag.BaseFragment;
import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.broadcastreceiver.BroadcastReceiverAction;
import com.dimong.www.loan.cache.UserIndexPageCache;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.widget.SettingDialog;
import com.google.gson.Gson;

public class MainFourFrag extends BaseFragment implements View.OnClickListener {
    private TextView tv_user_phone;
    private View ll_load;
    private View rl_kefu;
    private View rl_cooperate;
    private View rl_about_us;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastReceiverAction.REFRESH_USER)) {
                setUserInfo();
            }
        }
    };

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        ll_load = findViewById(R.id.ll_load);
        rl_kefu = findViewById(R.id.rl_kefu);
        rl_cooperate = findViewById(R.id.rl_cooperate);
        tv_user_phone = (TextView) findViewById(R.id.tv_user_phone);
        rl_about_us = findViewById(R.id.rl_about_us);
    }

    @Override
    public void setViewStatus() {
        ll_load.setOnClickListener(this);
        rl_kefu.setOnClickListener(this);
        rl_cooperate.setOnClickListener(this);
        rl_about_us.setOnClickListener(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastReceiverAction.REFRESH_USER);
        LocalBroadcastManager.getInstance(BaseApplication.getApplication()).registerReceiver(receiver, filter);
        setUserInfo();
    }

    private void setUserInfo() {
        if (UserIndexPageCache.getInstance().getUserIndexPageBean() == null) {
            tv_user_phone.setText("立即登陆");
        } else {
            tv_user_phone.setText(UserIndexPageCache.getInstance().getUserIndexPageBean().getPhone());
        }
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_main_four;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_load:
                if (!MyApplication.isLogin()) {
                    MyApplication.openLogin();
                }
                break;
            case R.id.rl_kefu:
                if (UserIndexPageCache.getInstance().getUserIndexPageBean() == null) {
                    UserIndexPageCache.getInstance().loadCache();
                    return;
                }
                if (TextUtils.isEmpty(UserIndexPageCache.getInstance().getUserIndexPageBean().getCustomer())) {
                    return;
                }
                final SettingDialog settingDialog = new SettingDialog(getActivity(), "客服热线", UserIndexPageCache.getInstance().getUserIndexPageBean().getCustomer(), new String[]{"取消", "呼叫"});
                settingDialog.setSettingDialogInterface(new SettingDialog.SettingDialogInterface() {
                    @Override
                    public void onCancelClick() {
                        settingDialog.cancel();
                    }

                    @Override
                    public void onOkClick() {
                        settingDialog.cancel();
                        Intent intent = new Intent(); // 意图对象：动作 + 数据
                        intent.setAction(Intent.ACTION_DIAL); // 设置动作
                        Uri data = Uri.parse("tel:" + UserIndexPageCache.getInstance().getUserIndexPageBean().getCustomer()); // 设置数据
                        intent.setData(data);
                        startActivity(intent);
                    }
                });
                settingDialog.show();
                break;
            case R.id.rl_cooperate:
                if (UserIndexPageCache.getInstance().getUserIndexPageBean() == null) {
                    UserIndexPageCache.getInstance().loadCache();
                    return;
                }
                if (TextUtils.isEmpty(UserIndexPageCache.getInstance().getUserIndexPageBean().getBusiness())) {
                    return;
                }
                final SettingDialog settingDialog1 = new SettingDialog(getActivity(), "商务合作", UserIndexPageCache.getInstance().getUserIndexPageBean().getBusiness(), new String[]{"确定"});
                settingDialog1.setSettingDialogInterface(new SettingDialog.SettingDialogInterface() {
                    @Override
                    public void onCancelClick() {
                        settingDialog1.cancel();
                    }

                    @Override
                    public void onOkClick() {
                        settingDialog1.cancel();
                    }
                });
                settingDialog1.show();
                break;
            case R.id.rl_about_us:
                if (UserIndexPageCache.getInstance().getUserIndexPageBean() == null) {
                    UserIndexPageCache.getInstance().loadCache();
                    return;
                }
                UIHelper.openWebViewAty(getActivity(),"关于我们",UserIndexPageCache.getInstance().getUserIndexPageBean().getAbout());
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(BaseApplication.getApplication()).unregisterReceiver(receiver);
    }
}
